<template>
  <div class="system-customerInfo-edit">
    <!-- 添加或修改客户信息;对话框 -->
    <el-dialog v-model="isShowDialog" width="800px" :close-on-click-modal="false" :destroy-on-close="true">
      <template #header>
        <div v-drag="['.system-customerInfo-edit .el-dialog', '.system-customerInfo-edit .el-dialog__header']">
          {{ (!formData.id || formData.id == 0 ? '添加' : '修改') + '客户信息;' }}</div>
      </template>
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="120px">
        <el-form-item label="客户编号" prop="customerId">
          <el-input v-model="formData.customerId" placeholder="请输入客户编号" />
        </el-form-item>
        <el-form-item label="客户名称" prop="customerName">
          <el-input v-model="formData.customerName" placeholder="请输入客户名称" />
        </el-form-item>
        <el-form-item label="联系人" prop="contactPerson">
          <el-input v-model="formData.contactPerson" placeholder="请输入联系人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="formData.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <CitySelector :required="true" v-model:province="formData.customerProvince" v-model:city="formData.customerCity"
          v-model:area="formData.customerCounty" />
        <el-form-item label="地址" prop="customerAddress">
          <el-input v-model="formData.customerAddress" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item  v-if="formData.id" label="状态" prop="customerStatus">
          <el-radio-group v-if="formData.id" v-model="formData.customerStatus">
            <el-radio v-for="dict in customer_status" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item v-if="!formData.id" label="密码" prop="password">
          <el-input type="password" v-model="formData.password" placeholder="请输入密码" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="onSubmit" :disabled="loading">确 定</el-button>
          <el-button @click="onCancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { reactive, toRefs, defineComponent, ref, unref, getCurrentInstance } from 'vue';
import { ElMessageBox, ElMessage } from 'element-plus';
import CitySelector from '/@/components/CitySelector/index.vue'
import {
  listCustomerInfo,
  getCustomerInfo,
  delCustomerInfo,
  addCustomerInfo,
  updateCustomerInfo,
} from "../../api/customer";
export default defineComponent({
  name: "ApiV1SystemCustomerInfoEdit",
  components: {
    CitySelector
  },
  setup(props, { emit }) {
    const { proxy } = getCurrentInstance()
    const formRef = ref(null);
    const menuRef = ref();
    const state = reactive({
      loading: false,
      isShowDialog: false,
      formData: {
        id: undefined,
        customerId: undefined,
        customerName: undefined,
        contactPerson: undefined,
        contactPhone: undefined,
        customerProvince: '',
        customerCity: '',
        customerCounty: '',
        customerAddress: undefined,
        customerStatus: '',
        password: undefined,
        tenantId: undefined,
        tenantName: undefined,
        createdBy: undefined,
        createdAt: undefined,
        updatedBy: undefined,
        updatedAt: undefined,
      },
      // 表单校验
      rules: {
        id: [
          { required: true, message: "序号不能为空", trigger: "blur" }
        ],
        customerId: [
          { required: true, message: "客户编号不能为空", trigger: "blur" }
        ],
        customerName: [
          { required: true, message: "客户名称不能为空", trigger: "blur" }
        ],
        contactPerson: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contactPhone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        customerProvince: [
          { required: true, message: "省份不能为空", trigger: "blur" }
        ],
        customerCity: [
          { required: true, message: "市不能为空", trigger: "blur" }
        ],
        customerCounty: [
          { required: true, message: "区县不能为空", trigger: "blur" }
        ],
        customerStatus: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
      }
    });
    // 打开弹窗
    const openDialog = (row) => {
      resetForm();
      if (row) {
        getCustomerInfo(row.customerId).then((res) => {
          const data = res.data;
          state.formData = data;
        })
      }
      state.isShowDialog = true;
    };
    // 字典选项数据        
    const {
      customer_status
    } = proxy.useDict(
      'customer_status'
    )
    // 关闭弹窗
    const closeDialog = () => {
      state.isShowDialog = false;
    };
    // 取消
    const onCancel = () => {
      closeDialog();
    };
    // 提交
    const onSubmit = () => {
      const formWrap = unref(formRef);
      if (!formWrap) return;
      formWrap.validate((valid) => {
        if (!state.formData.customerProvince || !state.formData.customerCity || !state.formData.customerCounty) {
          ElMessage.warning('请选择省市区')
          return
        }
        if (valid) {
          state.loading = true;
          if (!state.formData.id || state.formData.id === 0) {
            //添加
            addCustomerInfo(state.formData).then(() => {
              ElMessage.success('添加成功');
              closeDialog(); // 关闭弹窗
              emit('customerInfoList')
            }).finally(() => {
              state.loading = false;
            })
          } else {
            //修改
            updateCustomerInfo(state.formData).then(() => {
              ElMessage.success('修改成功');
              closeDialog(); // 关闭弹窗
              emit('customerInfoList')
            }).finally(() => {
              state.loading = false;
            })
          }
        }
      });
    };
    const resetForm = () => {
      state.formData = {
        id: undefined,
        customerId: undefined,
        customerName: undefined,
        contactPerson: undefined,
        contactPhone: undefined,
        customerProvince: '',
        customerCity: '',
        customerCounty: '',
        customerAddress: undefined,
        customerStatus: 'init',
        password: undefined,
        tenantId: undefined,
        tenantName: undefined,
        createdBy: undefined,
        createdAt: undefined,
        updatedBy: undefined,
        updatedAt: undefined,
      }
    };
    return {
      proxy,
      openDialog,
      closeDialog,
      onCancel,
      onSubmit,
      menuRef,
      formRef,
      customer_status,
      ...toRefs(state),
    };
  }
})
</script>
<style scoped>  .kv-label {
    margin-bottom: 15px;
    font-size: 14px;
  }

  .mini-btn i.el-icon {
    margin: unset;
  }

  .kv-row {
    margin-bottom: 12px;
  }</style>